package com.application.metroscanner;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;

import com.application.metroscanner.tools.Classifier;
import com.application.metroscanner.tools.FileManager;
import com.example.metroscanner.R;

public class MainActivity extends Activity {

	private Chronometer chronometer;
	private Button startButton;
	private Date date;
	private SimpleDateFormat dateFormatter;
	private Boolean isRunning;
	private Date startTime;

	private final Context context = this;
	private SharedPreferences sharedPreferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// shared preferences
		sharedPreferences = context.getSharedPreferences(
				getString(R.string.preference_file_key), Context.MODE_PRIVATE);
		// date formatter
		dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss",
				Locale.FRANCE);
		isRunning = false;

		// start button
		startButton = (Button) findViewById(R.id.startButton);
		// chronometer
		chronometer = (Chronometer) findViewById(R.id.chronometer);

		startButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				date = new Date();

				if (!isRunning) {
					// start chronometer
					chronometer.setBase(SystemClock.elapsedRealtime());
					chronometer.start();

					startButton.setText(R.string.startbutton_stop);
					isRunning = true;
					startTime = date;

					SharedPreferences.Editor editor = sharedPreferences.edit();
					editor.putString(getString(R.string.startTime),
							dateFormatter.format(date));
					boolean hasCommitted = editor.commit();

					if (!hasCommitted) {
						// toast
					}
				} else {
					// stop chronometer
					chronometer.setBase(SystemClock.elapsedRealtime());
					chronometer.stop();
					// reset data
					isRunning = false;
					startButton.setText(R.string.startbutton_start);
					SharedPreferences.Editor editor = sharedPreferences.edit();
					editor.remove(getString(R.string.startTime));
					editor.commit();
					// prompt dialog
					date = new Date();
					SubmitRideDialog rideDialog = new SubmitRideDialog(context,
							startTime, date);
					rideDialog.show();
				}
			}
		});
	}

	@Override
	protected void onStart() {
		super.onStart();
		// get saved date
		String savedStartTime = sharedPreferences.getString(
				getString(R.string.startTime), "");
		if (savedStartTime != "") {
			try {
				startTime = dateFormatter.parse(savedStartTime);
			} catch (ParseException e) {
				startTime = null;
			}
		}
		if (startTime != null) {
			isRunning = true;
			startButton.setText(R.string.startbutton_stop);
			date = new Date();
			long elapsedTime = date.getTime() - startTime.getTime();
			chronometer.setBase(SystemClock.elapsedRealtime() - elapsedTime);
			chronometer.start();
		} else {
			isRunning = false;
			startButton.setText(R.string.startbutton_start);
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		chronometer.stop();// save some memory :)
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_my_rides:
			openRideHistory();
			break;
		case R.id.action_my_graphics:
			openGraphics();
			break;
		default:
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/** Called when the user clicks the Send button */
	public void openRideHistory() {
		Intent intent = new Intent(this, RideHistoryActivity.class);
		startActivity(intent);
	}

	public void openGraphics() {
		Intent intent = new Intent(this, GraphicsActivity.class);
		startActivity(intent);
	}

	public void generateData(View view) {
		FileManager manager = FileManager.getInstance(this);
		manager.GenerateTestFile();
	}

	public void classifyData(View view) {
		FileManager manager = FileManager.getInstance(this);

		ArrayList<Classifier.Cluster> clusters = Classifier.classify(manager
				.ReadFile(getString(R.string.CSV_rides)));
		((Button) view).setText(clusters.size() + " classes");
	}
}
